﻿{//打印管理界面}
<extend name="Public/template/printerbase.html"/>
<block name='title'>{$title}</block>
<block name='header'>
<include file="./Public/template/printernav.html" manage="active"/>
</block>
<block name='content'>
<div class="title" >
    <h2>{$title}</h2>
</div>
<div class="container">
    <div class="row"><div class="col-lg-10">
    <div class="table-responsive">
    <table class="table">
        <thead>
            <tr>
                <th style="width:15%">待打印文件</th>
                <th>份数</th>
                <th>单双</th>
                <th>彩印</th>
                <th>PPT格式</th>
                <th>上传者</th>
                <th>上传时间</th>               
                <th>打印状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody id="tablehead">
            <volist name="data" id="vo">
            <tr file_id="{$vo.id}">
                
                <td><a fid="{$vo.id}" class="download" href="__URL__/download/?fid={$vo.id}" target="_blank">[{$vo.id}]{$vo.name}</a></td>                
               
         <if condition="$vo.copies eq 0">
             <td class="copies">现打</td><td class="double_side">-</td><td class="color">-</td><td class="ppt_layout">-</td>    
      <else/>
            <td class="copies"> {$vo.copies}</td><td class="double_side">
                <if condition="$vo.double_side gt 0">
                双面
                <else/>
                单面
                </if>
            </td>
            <td class="color">
                <if condition="$vo.color gt 0"> 彩印<else/>黑白</if>
            </td>
                <td class="ppt_layout">
                    {$vo.ppt_layout}
                </td>
            </if>
                <td class="phone">
                    <notempty name="vo['phone']">
                    <a tabindex="0" role="button" id="{$vo['id']}_phone" uid="{$vo['use_id']}" class="bind glyphicon glyphicon-phone" data-toggle="popover" data-trigger="focus" data-content="Loading..."></a>
                    </notempty>
                    {$vo.use_name}[{$vo.student_number}]
                </td>
                
                <td class="time">{$vo.time}</td>               
                
                <td class="status" id="{$vo['id']}_status" data="{$vo['status']}" copies="{$vo['copies']}">
                    <switch name="vo['status']">
                    <case value="1">尚未下载</case>
                    <case value="2">已下载</case>
                    <case value="3">正在打印</case>
                    <case value="4"><if condition="$vo.copies eq 0">已下载<else/>已印待付款</if></case>
                    <case value="5">已付款</case>
                    <default />未知状态
                    </switch>
                </td>
                
                
                <td>                    
                    <switch name="vo['status']">
                    <case value="1"><button type="button" id="{$vo.id}_button" fid='{$vo.id}' class="set btn btn-primary">通知已下载</button></case>
                    <case value="2" break="0"></case>
                    <case value="3"><button type="button" id="{$vo.id}_button" fid='{$vo.id}' class="set btn btn-success">通知已打印</button></case>
                    <case value="4"><button type="button" id="{$vo.id}_button" fid='{$vo.id}' class="set btn btn-warning">确认已付款</button><!--<notempty name="vo['phone']"><a role="button" class="send glyphicon glyphicon-phone"></a></notempty>--></case>
                    <default />不可操作
                    </switch>                   
                </td>
                
                
            </tr>
            </volist>
        </tbody>
    </table>
    </div>
    </div></div>
</div>
</block>
<block name='end'>
<script>
var double_side = new Array("单面","双面");
var color = new Array("黑白","彩色");
var f_status = new Array("未知状态","尚未下载","已下载","正在打印","已印待付款","已付款");
var b_class = new Array("set btn btn-primary","set btn btn-primary","set btn btn-success","set btn btn-success","set btn btn-warning");
var b_status = new Array("不可操作","通知已下载","通知已打印","通知已打印","确认已付款");

function insert(data)
{
    var file = $('tr:last').clone();
    file.attr('file_id',data.id);
    
    var link = file.find('.download');
    link.attr('href',"/Printer/File/download/?fid="+data.id);
    link.html('['+data.id+']'+data.name);
    
    var copies = file.find('.copies');
    if(data.copies==0)
    {
        copies.html('现打');
    }
    else
    {
        copies.html(data.copies);
    }
     
    var double = file.find('.double_side');
    double.html(double_side[data.double_side]);
    
    var color_node = file.find('.color');
    color_node.html(color[data.color]);

    var ppt_layout = file.find('.ppt_layout');
    ppt_layout.html(data.ppt_layout);

    var bind = file.find('.bind');
    var phone = file.find('.phone');
    bind.remove();
    phone.html(data.use_name+'['+data.student_number+']');
    if(data.phone)
    {
        var html = '<a tabindex="0" role="button" id="'+data.id+'_phone" uid="'+data.use_id+'" class="bind glyphicon glyphicon-phone" data-toggle="popover" data-trigger="focus" data-content="Loading..."></a>';
        phone.prepend(html);
    }
    
    var time = file.find('.time');
    time.html(data.time);
    
    var status = file.find('.status');
    status.html(f_status[data.status]);
    
    var btn = file.find('button');
    btn.removeClass().addClass(b_class[data.status]);
    btn.html(b_status[data.status]);
    $('tbody').prepend(file);
};

$("table").on('click','.download',function(e){
    var link = $(this);
    var file = link.parent().parent();
    var fid = file.attr('file_id');
    var URL = "/Printer/File/set";
    $.post(URL, {
        fid: fid,
        download: 1
    }, function(data) {
        if (data.status) {
            var btn =file.find('button');
            btn.removeClass().addClass(b_class[data.info.operation]);
            btn.html(b_status[data.info.operation]);
            file.find('.status').html(f_status[2]);
        } else {
            alert(data.info);
        }
    });
});


$("table").on('click', '.set', function() {
    var URL = "/Printer/File/set";
    var btn = $(this);
    btn.attr('disabled','disabled');
    var file=btn.parent().parent();
    var fid = file.attr('file_id');
    $.get(URL, {
        fid: fid
    }, function(data) {
        if (data.status) {
            if (data.info.status == 5) {
                file.hide(1600);
            } else {
                btn.removeClass().addClass(b_class[data.info.operation]);
                btn.html(b_status[data.info.operation]);
                file.find('.status').html(f_status[data.info.status]);
                /*var phone = file.find('.phone');
                if (data.info.status == 4 && data.info.copies!=0 &&phone) {
                    var html = '<a role="button" class="send glyphicon glyphicon-phone"></a>';
                    btn.after(html);
                }*/
            }
        } else {
            alert(data.info);
        }
        btn.removeAttr('disabled');
    });
});

var phoneIsLoad =new Array();

$("table").on('click', '.bind', function(e) {
    var URL = "/Printer/File/bind";
    var btn = $(this);
    var uid = btn.attr('uid');
    if(phoneIsLoad[uid]===undefined)
    {
        $.get(URL, {
            uid : uid 
        }, function(data) {
            if (data.status) {
                btn.attr('data-content',data.info);
                btn.popover('show');
                phoneIsLoad[uid]=data.info;
            }
        });
    }
    else
    {
        btn.attr('data-content',phoneIsLoad[uid]);
        btn.popover('show');
    }
});

isRefreshing = false;

function refresh() {
    if (isRefreshing) {
        return;
    } else {
        isRefreshing = true;
    }
    var file_id = $('tbody').children().attr('file_id');
    var url = "__URL__/refresh";
    $.post(url, {
        file_id: file_id
    }, function(data) {
        if (data.status) {
            for(var key in data.info)
            {
                insert(data.info[key]);
            }
            var html='<div class="alert alert-warning" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span></button><span class="text-danger">新文件到达</span></div>';
            $('main').prepend(html);
        }
        isRefreshing = false;
    });
}
$(document).ready(function() {
    setInterval(refresh, 15000);
});
</script>
</block>
